package while_lang.ast.exps;

import java.util.Random;

import while_lang.ast.Exp;
import while_lang.semantics.State;
import while_lang.semantics.SymbolTable;
import while_lang.semantics.Type;
import while_lang.semantics.types.IntegerType;
import while_lang.semantics.types.NumberType;

/**
 * Nodo del AST de While que representa la expresión aritmética de suma.
 * 
 * @author Leonardo Val
 */
public class Addition extends Exp {
	public final Exp operand1, operand2;

	public Addition(Exp operand1, Exp operand2) {
		this.operand1 = operand1;
		this.operand2 = operand2;
	}

	@Override
	public String unparse() {
		return "(" + operand1.unparse() + " + " + operand2.unparse() + ")";
	}

	@Override
	public Object evaluate(State state) {
		Object value1 = operand1.evaluate(state);
		Object value2 = operand2.evaluate(state);
		if (IntegerType.TYPE.isType(value1) && IntegerType.TYPE.isType(value2)) {
			return ((Integer) value1) + ((Integer) value2);
		} else {
			value1 = NumberType.TYPE.coerce(value1);
			value2 = NumberType.TYPE.coerce(value2);
			return ((Double) value1) + ((Double) value2);
		}
	}

	@Override
	public Type checkTypes(SymbolTable table) {
		/*
		 * Si ambos operandos son enteros el resultado debe ser entero. Si ambos
		 * operandos son numéricos el resultado debe ser numérico. De no ser
		 * así hay que reportar un error de tipos.
		 */
		Type type1 = operand1.checkTypes(table);
		Type type2 = operand2.checkTypes(table);

		if (IntegerType.TYPE.isType(type1) && IntegerType.TYPE.isType(type2)) {
			return IntegerType.TYPE;
		}
		if (NumberType.TYPE.isType(type1) && NumberType.TYPE.isType(type2)) {
			return NumberType.TYPE;
		}
		if (type1.isCompatible(type2)) {
			if (IntegerType.TYPE.isType(type2)) {
				return IntegerType.TYPE;
			} else {
				if (NumberType.TYPE.isType(type2)) {
					return NumberType.TYPE;
				}
			}
		}
		table.errors.add("Error de tipos en la suma.");
		return IntegerType.TYPE;
	}

	public static Addition generate(Random random, int min, int max) {
		return new Addition(Exp.generate(random, min - 1, max - 1),
				Exp.generate(random, min - 1, max - 1));
	}

	@Override
	public String toString() {
		return "Addition(" + operand1 + ", " + operand2 + ")";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((operand1 == null) ? 0 : operand1.hashCode());
		result = prime * result
				+ ((operand2 == null) ? 0 : operand2.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Addition other = (Addition) obj;
		if (operand1 == null) {
			if (other.operand1 != null)
				return false;
		} else if (!operand1.equals(other.operand1))
			return false;
		if (operand2 == null) {
			if (other.operand2 != null)
				return false;
		} else if (!operand2.equals(other.operand2))
			return false;
		return true;
	}

}
